package com.uzykj.modules.front.web;

import com.uzykj.common.json.AjaxJson;
import com.uzykj.common.utils.CacheUtils;
import com.uzykj.common.utils.StringUtils;
import com.uzykj.core.persistence.Page;
import com.uzykj.core.web.BaseController;
import com.uzykj.modules.blog.entity.BlogCategory;
import com.uzykj.modules.blog.entity.BlogNote;
import com.uzykj.modules.blog.entity.BlogReview;
import com.uzykj.modules.blog.service.BlogNoteService;
import com.uzykj.modules.blog.service.BlogReviewService;
import com.uzykj.modules.front.entity.FollowUser;
import com.uzykj.modules.front.service.FollowUserService;
import com.uzykj.modules.sys.entity.User;
import com.uzykj.modules.sys.mapper.UserMapper;
import com.uzykj.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ghostxbh
 * @date 2020/8/16
 * @description
 */
@Controller
@RequestMapping(value = "${frontPath}/u")
public class AuthorController extends BaseController {
    @Autowired
    private BlogNoteService blogNoteService;

    @Autowired
    private BlogReviewService blogReviewService;

    @Autowired
    private FollowUserService followUserService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "follow", method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson follow(@RequestParam("status") String status,
                           @RequestParam("followed") String followed) {
        AjaxJson j = new AjaxJson();
        try {
            User user = UserUtils.getUser();
            if (user != null && !StringUtils.isNoneBlank(user.getId())) {
                if ("follow".equals(status)) {
                    followUserService.add(user.getId(), followed);
                    j.setSuccess(true);
                    j.setMsg("已关注");
                } else if ("unfollow".equals(status)) {
                    followUserService.del(user.getId(), null);
                    j.setSuccess(true);
                    j.setMsg("已取消关注");
                }
            } else {
                j.setSuccess(false);
                j.setMsg("loginFail");
            }
        } catch (Exception e) {
            logger.error("add follow error", e);
            j.setSuccess(false);
            j.setMsg("添加关注失败");
        }
        return j;
    }

    @RequestMapping(value = "author", method = RequestMethod.GET)
    public String author(@RequestParam(value = "name", required = false) String name,
                         @RequestParam("id") String id, Model model,
                         HttpServletRequest request, HttpServletResponse response) {
        List<BlogCategory> categoryList = (List<BlogCategory>) CacheUtils.get(UserUtils.BLOG_CACHE, UserUtils.CACHE_BLOG_LIST);
        Page<BlogNote> page = new Page<BlogNote>(request, response);
        page.setPageSize(10);
        page.setPageNo(1);
        BlogNote note = new BlogNote();
        note.setCreateBy(new User(id));
        Page<BlogNote> notePage = blogNoteService.findPage(page, note);

        List<User> fanses = followUserService.fanses(id);
        List<User> followers = followUserService.followers(id);

        Page<BlogReview> blogReviewPage = new Page<BlogReview>(0, 5);
        blogReviewPage.setOrderBy("create_date desc");
        BlogReview review = new BlogReview();
        review.setReviewer(new User(id));
        Page<BlogReview> reviewPage = blogReviewService.findPage(blogReviewPage, review);
        User user = UserUtils.get(id);

        Page<BlogNote> blogNotePage = new Page<BlogNote>(0, 10);
        blogNotePage.setOrderBy("a.create_date desc");
        BlogNote blogNote = new BlogNote();
        blogNote.setCreateBy(new User(id));

        long count = blogNoteService.count(blogNote);

        Page<BlogNote> newNoteList = blogNoteService.findPage(blogNotePage, blogNote);



        model.addAttribute("categoryList", categoryList);
        model.addAttribute("user", user);
        model.addAttribute("notePage", notePage);
        model.addAttribute("fanses", fanses);
        model.addAttribute("followers", followers);
        model.addAttribute("newNoteList", newNoteList.getList());
        model.addAttribute("noteCount", count);
        model.addAttribute("reviews", reviewPage.getList());
        return "modules/front/personal";
    }
}
